package mapper

import (
	"context"
	"gorm.io/gorm"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/initialize/mysql"
	"mall/services/product/rpc/internal/mapper/entity"
)

var spuSkuAttrValueAllColumn = "spu_sku_attr_id, create_time, update_time, spu_id, sku_id, attr_id, attr_name, " +
	"attr_value_id, attr_value_name, status"

type SpuSkuAttrValueMapper struct {
}

func NewSpuSkuAttrValueMapper() *SpuSkuAttrValueMapper {
	return &SpuSkuAttrValueMapper{}
}

func (m *SpuSkuAttrValueMapper) SelectCountCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SpuSkuAttrValueBO) (totalCount int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var total int64
	userEntity := entity.SpuSkuAttrValueEntity{}
	tx = tx.Select(spuSkuAttrValueAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.Count(&total)
	return total, tx.Error
}

func (m *SpuSkuAttrValueMapper) SelectOneCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SpuSkuAttrValueBO) (valueEntity entity.SpuSkuAttrValueEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	m.getAllWhereSql(tx, skuStoreBo)
	userEntity := entity.SpuSkuAttrValueEntity{}
	tx = tx.Select(spuSkuAttrValueAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.First(&userEntity)
	return userEntity, tx.Error
}
func (m *SpuSkuAttrValueMapper) SelectListCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SpuSkuAttrValueBO) (valueEntityArr []entity.SpuSkuAttrValueEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#SelectListCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var spuSkuAttrArr []entity.SpuSkuAttrValueEntity
	userEntity := entity.SpuSkuAttrValueEntity{}
	tx = tx.Select(spuSkuAttrValueAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.Find(&spuSkuAttrArr)
	return spuSkuAttrArr, tx.Error
}

func (m *SpuSkuAttrValueMapper) getAllWhereSql(db *gorm.DB, storeBo bo.SpuSkuAttrValueBO) {
	if storeBo.SpuSkuAttrId > 0 {
		db.Where("spu_sku_attr_id = ?", storeBo.SpuSkuAttrId)
	}
	if storeBo.SpuId > 0 {
		db.Where("spu_id = ?", storeBo.SpuId)
	}
	if storeBo.SkuId > 0 {
		db.Where("sku_id = ?", storeBo.SkuId)
	}
	if storeBo.AttrId > 0 {
		db.Where("attr_id = ?", storeBo.AttrId)
	}
	if len(storeBo.AttrName) > 0 || storeBo.AttrName != "" {
		db.Where("attr_name = ?", storeBo.AttrName)
	}
	if storeBo.AttrValueId > 0 {
		db.Where("attr_value_id = ?", storeBo.AttrValueId)
	}
	if len(storeBo.AttrValueIdArr) > 0 {
		db.Where("attr_value_id in ?", storeBo.AttrValueIdArr)
	}
	if len(storeBo.AttrValueName) > 0 || storeBo.AttrValueName != "" {
		db.Where("attr_value_name = ?", storeBo.AttrValueName)
	}
	if storeBo.Status > 0 {
		db.Where("status = ?", storeBo.Status)
	}
	if storeBo.StartTime != nil {
		db.Where("create_time = ?", storeBo.StartTime)
	}
	if storeBo.EndTime != nil {
		db.Where("create_time = ?", storeBo.EndTime)
	}
}

func (m *SpuSkuAttrValueMapper) BatchInsertCtx(ctx context.Context, engin *gorm.DB, arr []entity.SpuSkuAttrValueEntity) (idArr []int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#BatchInsertCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Create(&arr)
	var ids []int64
	for _, v := range arr {
		ids = append(idArr, v.SkuId)
	}
	return ids, tx.Error
}

func (m *SpuSkuAttrValueMapper) DeleteCtx(ctx context.Context, engin *gorm.DB, id []int64) (err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#DeleteCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	spuAttrValueEntity := entity.SpuSkuAttrValueEntity{}
	// db.Where("id in ?", []int{18, 19, 20}).Delete(&TestUser{})
	tx := engin.Where("spu_sku_attr_id in ?", id).Delete(spuAttrValueEntity, id)
	return tx.Error
}

func (m *SpuSkuAttrValueMapper) BatchInsert(ctx context.Context, engin *gorm.DB, arr []entity.SpuSkuAttrValueEntity) (idArr []int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuSkuAttrValueMapper#BatchInsert")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Create(&arr)
	var ids []int64
	for _, v := range arr {
		ids = append(idArr, v.SpuSkuAttrId)
	}
	return ids, tx.Error
}
